package cdu.hjx.project.sys.handler;

import cdu.hjx.project.pojo.Employee;
import cdu.hjx.project.pojo.EmpsRoles;
import cdu.hjx.project.pojo.Role;
import cdu.hjx.project.sys.service.EmpRoleService;
import cdu.hjx.project.sys.service.EmployeeService;
import cdu.hjx.project.sys.service.RoleService;
import cdu.hjx.project.utils.CommonResult;
import cdu.hjx.project.utils.DeterministicAesEncryption;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/emp")
public class EmpployeeHandler {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private RoleService roleService;
    @Resource
    private EmpRoleService empRoleService;

    /**
     * 添加员工
     * @param employee 员工信息
     * @return CommonResult 统一返回类
     */
    @PostMapping("/add")
    public CommonResult addEmployee(@RequestBody Employee employee) {
        try {
            employee.setUcreate_by("sys");
            employee.setUupdate_by("sys");
            int i = employeeService.insert(employee);
            switch (i) {
                case 1:
                    return CommonResult.success(employee);
                case 0:
                    return CommonResult.error(201, "添加员工失败,请重试.");
                case -1:
                    return CommonResult.error(-1, "您要添加的员工已经存在,请换一个.");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,添加员工失败,请联系管理员.");
        }
        return null;
    }
    @GetMapping("/{pn}/{size}")
    public CommonResult<PageInfo<Employee>> emps (@PathVariable("pn") int pn, @PathVariable("size") int size) {
        try {
            List<Employee> employees = employeeService.employees();
            // 遍历员工集合，目的是为了拿到每一个员工的id，此外，还需要拿上员工的id去查询该员工的角色
            for (int i = 0; i < employees.size(); i++) {
                Employee employee = employees.get(i);
                long eid = employee.getId(); // 拿上员工ID去查角色的信息
                EmpsRoles empsRoles = empRoleService.findRoleIdByEid(eid); // 当前对象有可能是空的，所以要判断
                if (null != empsRoles) {
                    long id = empsRoles.getRid();
                    Role role = roleService.findRoleById((int) id);
                    employee.setRole(role);
                }

            }
            PageInfo<Employee> pageInfo = new PageInfo<>(employees);
            if (null != employees) {
                return CommonResult.success(pageInfo);
            }
            return CommonResult.error(401, "请求成功,但没有对应数据.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,查询失败,请联系管理员.");
        }
    }

    @PostMapping("/login")
    public CommonResult login(@RequestParam("username") String username, @RequestParam("password") String  password, @RequestParam("rid") int rid) {
        try {
            // 通过账号和密码得到了员工对象
            Employee employee = employeeService.login(username, password);
            if (null != employee) {
                // 员工信息是对的，但是接下来继续判断他的角色到底对不对
                EmpsRoles empsRoles = empRoleService.findRoleIdByEid(employee.getId());
                // 拿上empsRoles对象中的rid去查询角色信息

                Role role = roleService.findRoleById((int) empsRoles.getRid());
                if (rid == role.getId()) {
                    if (null != role) {
                        employee.setRole(role); // 账号密码是正确的并且角色也是正确的
                        return CommonResult.success(employee);
                    } else {
                        return CommonResult.error(202, "您的角色不正确,请联系管理员.");
                    }
                } else {
                    return CommonResult.error(202, "您的角色不正确,请联系管理员.");
                }
            } else {
                return CommonResult.error(201, "账号或密码错误,请重新输入.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,登录失败,请联系管理员.");
        }
    }
}
